package com.leo.oplayer.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts;
import android.util.Log;

public class PersonUtil {
	
	private static final String TAG = "PersonUtil";

	private static final String[] PEOPLE_PROJECTION = new String[] {
			Contacts.People._ID, Contacts.People.PRIMARY_PHONE_ID,
			Contacts.People.TYPE, Contacts.People.NUMBER,
			Contacts.People.LABEL, Contacts.People.NAME, };
	
	/**
	 * 根据姓名查id
	 * @param context
	 * @param name
	 * @return
	 */
	public static String findIdByName(Context context,String name) {
		return findByArg(context,Contacts.People.NAME,name,Contacts.People._ID);
	}

	/**
	 * 根据号码查id
	 * @param context
	 * @param number
	 * @return
	 */
	public static String findIdByNumber(Context context,String number) {
		
		ContentResolver content = context.getContentResolver();
		Cursor cursor = null;
		
        try{
        	cursor = content.query(Contacts.People.CONTENT_URI,
        			PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER);
            if(cursor != null && cursor.moveToFirst()){     
            	while(!cursor.isAfterLast()){
            		String tempNumber = cursor.getString(cursor.getColumnIndex(Contacts.People.NUMBER));
            		String personId = cursor.getString(cursor.getColumnIndex(Contacts.People._ID));            		
            		if(number.equals(formatNumber(tempNumber))){
            			return personId;
            		}
            		cursor.moveToNext();
            	}            	
            }
        }catch(Exception e){
        	Log.e(TAG, "find person error!");
        	return null;
        }finally{
        	if(cursor!=null && !cursor.isClosed()){
        		cursor.close();
        	}
        }
        return null;
	}
	
	
	/**
	 * 通过id查姓名
	 * @param context
	 * @param id
	 * @return
	 */
	public static String findNameById(Context context,String id){
		return findByArg(context,Contacts.People._ID,id,Contacts.People.NAME);
	}
	
	private static String findByArg(Context context,String key,String value,String retKey){
		String selection = null;
		if(Contacts.People._ID.equals(key)){
			//这里要写明表名是people
			selection = "people._id =" + value;
		}else if(Contacts.People.NAME.equals(key)){
			selection = Contacts.People.NAME + "='"+value+"'";
		}
		
		ContentResolver content = context.getContentResolver();
		Cursor cursor = null;
		
        try{
        	cursor = content.query(Contacts.People.CONTENT_URI,
        			PEOPLE_PROJECTION, selection, null, Contacts.People.DEFAULT_SORT_ORDER);
            if(cursor != null && cursor.moveToFirst()){        	
            	return cursor.getString(cursor.getColumnIndex(retKey));
            }
        }catch(Exception e){
        	Log.e(TAG, "find person error!");
        	return null;
        }finally{
        	if(cursor!=null && !cursor.isClosed()){
        		cursor.close();
        	}
        }
        return null;
	}
	
	private static String formatNumber(String number){
		return  number.replaceAll("-", "");
	}

}
